import re

from flask import Blueprint,request,make_response,jsonify,redirect
from flask_restful import reqparse
from util.myjwt import myjwt
from util.captcha.captcha import captcha
import json, random, time, requests
from util.myredis import r
from util.smscode import sms_code
from models.user import *

classidy_blue = Blueprint("classidyblue",__name__)

# @classidy_blue.route("/getmenu")
# def getmenu():
#     classify = db.session.execute("select * from classify where pid=0")
#     # type = classify.query.filter_by(pid=0).all()
#     # type = list(type)
#     # print(type)
#     clist = list(classify)
#
#     print(clist)
#     list1 = []
#     for i in list1:
#         list1.append({'id': i[0], 'name': i[3]})
#     return jsonify({'code': 200, 'data': list1})

@classidy_blue.route("/getone")
def getone():
    classify = db.session.execute("select * from classify")
    clist = list(classify)
    list1 = []
    menulist = []
    for i in clist:
        if i[4] == 0:
            if i[4] not in menulist:
                list1.append({'id': i[0], 'name': i[3], 'children': []})
                menulist.append([i[0]])
    for j in clist:
        for index,p in enumerate(list1):
            if j[4] == p['id']:
                list1[index]['children'].append({'id': j[0], 'name': j[3],'pid':j[4],'children1':[]})
    for x in clist:
        for index,p in enumerate(list1):
            aa = list1[index]['children']
            for index,t in enumerate(aa):
                if x[4] == t['id']:
                    aa[index]['chlidren'].append({'id': x[4], 'name': x[3]})
    print(list1)
    return jsonify({'code': 200, 'data': list1})

@classidy_blue.route("/setbasic", methods=['POST'])
def setbasic():
    data = request.data
    if data:
        data = json.loads(data)
        userid = data['userid']
        name = data['name']
        sex = data['sex']
        identity = data['identity']
        addr = data['addr']
        email = data['email']
        bas = basic.query.filter_by(userid=userid).first()
        if bas:
            bas.name = name
            bas.sex = sex
            bas.identity = identity
            bas.addr = addr
            bas.email = email
            db.session.add(bas)
            db.session.commit()
            return jsonify({'code': 200, 'msg': '修改成功'})
        bsc = basic(name=name,sex=sex,identity=identity,addr=addr,email=email,userid=userid)
        db.session.add(bsc)
        db.session.commit()
        return jsonify({'code': 200, 'msg': 'ok'})
    else:
        return jsonify({'code': 300, 'msg': '输入信息'})

# 查找当前用户信息是否存在
@classidy_blue.route("/getbasic")
def getbasic():
    userid = request.args.get('userid')
    bas =basic.query.filter_by(userid=userid).first()
    print(bas.id)
    list = []
    list.append({'id': bas.id, 'name': bas.name, 'sex': bas.sex, 'identity': bas.identity, 'addr': bas.addr, 'email': bas.email})
    print(list)
    return jsonify({'code': 200, 'list': list})



@classidy_blue.route("/setsere", methods=['POST'])
def setserve():
    data = request.data
    if data:
        data = json.loads(data)
        userid = data['userid']
        ability = data['ability']
        skil = data['skil']
        pay = data['pay']
        close = data['close']
        work = data['work']
        worktime = data['worktime']
        bas = serve.query.filter_by(userid=userid).first()
        if bas:
            bas.ability = ability
            bas.skil = skil
            bas.pay = pay
            bas.close = close
            bas.work = work
            bas.worktime = worktime
            db.session.add(bas)
            db.session.commit()
            return jsonify({'code': 200, 'msg': '修改成功'})
        bsc = serve(ability=ability,skil=skil,pay=pay,close=close,work=work,worktime=worktime)
        db.session.add(bsc)
        db.session.commin()
        return jsonify({'code':200,'msg':'ok'})
    else:
        return jsonify({'code':300,'msg':'输入信息'})


































